﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MiniFox.Infrastructure
{

    /// <summary>
    /// 列
    /// </summary>
    public class ViewColumn
    {
        string _name;
        string _sourceName;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sourceName"></param>
        public ViewColumn(string name, string sourceName)
        {
            _name = name;
            _sourceName = sourceName;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("name")]
        public string Name
        {
            get { return _name; }
        }
        /// <summary>
        /// 显示名
        /// </summary>
        [JsonProperty("displayName")]
        public string DisplayName { get; set; }
        /// <summary>
        /// 是否为主键
        /// </summary>
        [JsonProperty("isKey")]
        public bool IsKey { get; set; }
        /// <summary>
        /// 排序方向
        /// </summary>
        [JsonProperty("sortDesc")]
        public bool? SortDesc { get; set; }
        /// <summary>
        /// 类型
        /// </summary>
        [JsonProperty("dataType")]
        public string DataType { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("displayFormat")]
        public string DisplayFormat { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("ordinal")]
        public int Ordinal { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="viewColumn"></param>
        public static implicit operator DataColumn(ViewColumn viewColumn)
        {
            DataColumn column = new DataColumn(viewColumn.Name);
            column.Caption = viewColumn.DisplayName;
            column.Unique = viewColumn.IsKey;
            column.SetOrdinal(viewColumn.Ordinal);
            return column;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="column"></param>
        public static implicit operator ViewColumn(DataColumn column)
        {
            ViewColumn viewColumn = new ViewColumn(column.ColumnName, column.ColumnName);
            viewColumn.DisplayName = column.Caption;
            viewColumn.IsKey = column.Unique;
            viewColumn.Ordinal = column.Ordinal;
            return viewColumn;
        }
    }

}
